package com.shop.loan.util;//package com.shop.loan.util;
//
//import cn.hutool.core.date.DateUtil;
//import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
//import com.shop.loan.mapper.LoanOrderByMybatisMapper;
//import com.shop.loan.mapper.LoanOrderPayLogByMybatisMapper;
//import com.shop.loan.model.common.Code;
//import com.shop.loan.model.common.ResResult;
//import com.shop.loan.model.entity.LoanOrderBillByMybatis;
//import com.shop.loan.model.entity.LoanOrderByMybatis;
//import com.shop.loan.model.entity.LoanOrderPayLogByMybatis;
//import com.shop.loan.model.enums.OrderStatus;
//import com.shop.loan.model.enums.PayFormType;
//import com.shop.loan.model.enums.PaymentType;
//import com.shop.loan.redis.RedisCache;
//import com.shop.loan.redis.RedisLockUtil;
//import com.shop.loan.redis.RedisUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import redis.clients.jedis.Jedis;
//
//import javax.annotation.Resource;
//import javax.transaction.Transactional;
//import java.math.BigDecimal;
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
//@Slf4j
//@Component
//public class PayUtil {
//
//    @Resource
//    RedisUtil redisUtil;
//
//    @Resource
//    RedisLockUtil redisLockUtil;
//
//    @Resource
//    PayOrderByMybatisMapper payOrderByMybatisMapper;
//
//    @Resource
//    LoanOrderByMybatisMapper loanOrderByMybatisMapper;
//
//    @Resource
//    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;
//
//    @Resource
//    LoanOrderPayLogByMybatisMapper loanOrderPayLogByMybatisMapper;
//
//    /**
//     * 支付回调统一处理
//     *
//     * @param trade_status
//     * @param trade_no
//     * @param out_trade_no
//     * @return
//     */
//    @Transactional
//    public Boolean payBack(String trade_status, String trade_no, String out_trade_no) {
//        PayOrderByMybatis payOrder = payOrderByMybatisMapper.selectByOutTradeNo(out_trade_no);
//        if (payOrder == null || payOrder.getId() == null) {
//            return false;
//        }
//        PayOrderByMybatis updatePayOrder = new PayOrderByMybatis();
//        updatePayOrder.setId(payOrder.getId());
//        if (StringUtils.hasLength(trade_no) && !StringUtils.hasLength(payOrder.getTradeNo())) {
//            updatePayOrder.setTradeNo(trade_no);
//        }
//        //订单已经成功
//        if ("TRADE_SUCCESS".trim().equals(payOrder.getStatus())) {
//            return false;
//        }
//        /**
//         * 交易状态
//         *
//         * WAIT_BUYER_PAY
//         * TRADE_CLOSED
//         * TRADE_SUCCESS
//         * TRADE_FINISHED
//         */
//        if ("TRADE_SUCCESS".trim().equals(trade_status)) {
//            //支付成功
//            updatePayOrder.setStatus(trade_status);
//            updatePayOrder.setPayTime(new Date());
//            payOrderByMybatisMapper.updateByPrimaryKeySelective(updatePayOrder);
//            /**
//             * 还款操作
//             */
//            PaymentType paymentType = PaymentType.online_alipay;
//            if (payOrder.getPayFrom().equals(PayFormType.alipay.getType())) {
//                paymentType = PaymentType.online_alipay;
//            } else if (payOrder.getPayFrom().equals(PayFormType.wx.getType())) {
//                paymentType = PaymentType.online_wx;
//            }
//            payment(payOrder.getReceiptAmount(), payOrder.getOrderId(), paymentType, trade_no, null);
//        }
//        return null;
//    }
//
//    /**
//     * 还款操作
//     *
//     * @param money
//     * @param orderId
//     * @param paymentType
//     * @param payOrderId
//     * @param remark
//     * @return
//     */
//    public ResResult payment(BigDecimal money, Long orderId, PaymentType paymentType, String payOrderId, String remark) {
//        /**
//         * 还款
//         */
//        if (money == null || orderId == null || money.doubleValue() <= 0) {
//            return new ResResult(Code.PARAM_REJECT);
//        }
//
//        Jedis jedis = redisUtil.getJedis();
//        String lockKey = RedisCache.orderPaymentLock + orderId;
//        String lockValue = UUID.randomUUID().toString();
//        try {
//            while (!redisLockUtil.lock(jedis, lockKey, lockValue)) {
//                Thread.sleep(50);
//            }
//            LoanOrderByMybatis loanOrderByMybatis = loanOrderByMybatisMapper.selectByPrimaryKey(orderId);
//            if (loanOrderByMybatis == null || loanOrderByMybatis.getId() == null) {
//                return new ResResult(Code.PARAM_REJECT);
//            }
//            if (loanOrderByMybatis.getStatus().intValue() != OrderStatus.underway.getStatus().intValue()) {
//                return new ResResult(Code.ORDER_STATUS_NOT_PAY);
//            }
//
//            if (loanOrderByMybatis.getRateMoney() == null) {
//                loanOrderByMybatis.setRateMoney(BigDecimal.ZERO);
//            }
//            if (loanOrderByMybatis.getPayMoney() == null) {
//                loanOrderByMybatis.setPayMoney(BigDecimal.ZERO);
//            }
//
//            if (money.add(loanOrderByMybatis.getPayMoney()).compareTo(loanOrderByMybatis.getMoney().add(loanOrderByMybatis.getRateMoney())) > 0) {
//                //还款金额大于本息金额
//                return new ResResult(Code.PAY_MONEY_TO_MANY);
//            }
//
//            LoanOrderByMybatis updateOrder = new LoanOrderByMybatis();
//            updateOrder.setId(loanOrderByMybatis.getId());
//            updateOrder.setPayMoney(loanOrderByMybatis.getPayMoney().add(money));
//            if (updateOrder.getPayMoney().compareTo(loanOrderByMybatis.getMoney().add(loanOrderByMybatis.getRateMoney())) >= 0) {
//                updateOrder.setStatus(OrderStatus.end.getStatus());
//                updateOrder.setFinishTime(new Date());
//                updateOrder.setOverdueStatus(true);
//            }
//            loanOrderByMybatisMapper.updateByPrimaryKeySelective(updateOrder);
//
//            BigDecimal opMoney = money;
//            Long billId = null;
//            //归还每期金额
//            List<LoanOrderBillByMybatis> signOrderBillByMybatisList = loanOrderBillByMybatisMapper.selectListByOrder(loanOrderByMybatis.getId());
//            for (LoanOrderBillByMybatis signOrderBillByMybatis : signOrderBillByMybatisList) {
//                if (signOrderBillByMybatis.getPayStatus()) {
//                    continue;
//                }
//                if (opMoney.doubleValue() <= 0) {
//                    continue;
//                }
//                BigDecimal unPayMoney = signOrderBillByMybatis.getMoney().add(signOrderBillByMybatis.getRateMoney()).subtract(signOrderBillByMybatis.getPayMoney());
//                if (opMoney.compareTo(unPayMoney) >= 0) {
//                    signOrderBillByMybatis.setPayMoney(signOrderBillByMybatis.getPayMoney().add(unPayMoney));
//                    signOrderBillByMybatis.setPayStatus(true);
//                    opMoney = opMoney.subtract(unPayMoney);
//                } else {
//                    signOrderBillByMybatis.setPayMoney(signOrderBillByMybatis.getPayMoney().add(opMoney));
//                    opMoney = BigDecimal.ZERO;
//                }
//                billId = signOrderBillByMybatis.getId();
//                signOrderBillByMybatis.setOpTime(new Date());
//                loanOrderBillByMybatisMapper.updateByPrimaryKeySelective(signOrderBillByMybatis);
//            }
//            //插入销账记录
//            LoanOrderPayLogByMybatis signOrderPayLogByMybatis = new LoanOrderPayLogByMybatis();
//            signOrderPayLogByMybatis.setOrderId(orderId);
//            signOrderPayLogByMybatis.setUserId(loanOrderByMybatis.getUserId());
//            signOrderPayLogByMybatis.setCreateTime(new Date());
//            signOrderPayLogByMybatis.setMoney(money);
//            signOrderPayLogByMybatis.setBillId(billId);
//            signOrderPayLogByMybatis.setType(paymentType.getType());
//            signOrderPayLogByMybatis.setRemark(remark);
//            signOrderPayLogByMybatis.setPayOrderId(payOrderId);
//            loanOrderPayLogByMybatisMapper.insertSelective(signOrderPayLogByMybatis);
//
//            //重新更新订单是否逾期
//            refreshOverdue(orderId);
//
//            return new ResResult(Code.SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error(Util.GetErrorDetails(e));
//        } finally {
//            if (jedis != null) {
//                redisLockUtil.release(jedis, lockKey, lockValue);
//                jedis.close();
//            }
//        }
//        return new ResResult(Code.FAIL);
//    }
//
//
//    /**
//     * 销账后 更改订单状态
//     *
//     * @param orderId
//     */
//    public void refreshOverdue(Long orderId) {
//        boolean isOverdue = false;
//        Date today = DateUtil.beginOfDay(new Date());
//        LoanOrderByMybatis loanOrderByMybatis = loanOrderByMybatisMapper.selectByPrimaryKey(orderId);
//        if (loanOrderByMybatis == null) {
//            return;
//        }
//        if (loanOrderByMybatis.getStatus() != OrderStatus.underway.getStatus().intValue()) {
//            return;
//        }
////        if (DateUtil.compare(loanOrderByMybatis.getLoanEnd(), today) < 0) {
////            isOverdue = true;
////        }
//        List<LoanOrderBillByMybatis> loanOrderBillByMybatisList = loanOrderBillByMybatisMapper.selectListByOrder(orderId);
//        if (loanOrderBillByMybatisList != null) {
//            for (LoanOrderBillByMybatis loanOrderBillByMybatis : loanOrderBillByMybatisList) {
//                if (loanOrderBillByMybatis.getPayStatus()) {
//                    continue;
//                }
//                LoanOrderBillByMybatis updateBill = new LoanOrderBillByMybatis();
//                updateBill.setId(loanOrderBillByMybatis.getId());
//                if (DateUtil.compare(loanOrderBillByMybatis.getPayTime(), today) < 0) {
//                    updateBill.setOverdueStatus(false);
//                    isOverdue = true;
//                } else {
//                    updateBill.setOverdueStatus(true);
//                }
//                loanOrderBillByMybatisMapper.updateByPrimaryKeySelective(updateBill);
//            }
//        }
//        LoanOrderByMybatis updateOrder = new LoanOrderByMybatis();
//        updateOrder.setId(orderId);
//        updateOrder.setOverdueStatus(isOverdue);
//        loanOrderByMybatisMapper.updateByPrimaryKeySelective(updateOrder);
//    }
//
//
//}
